﻿@{
    Layout = null;
    var returnUrl = Request.QueryString["returnUrl"];
    bool isLogin = ViewBag.isLogin;
    bool isSeas = ViewBag.isSeas;
}
@model shipping_address_dto
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>收货地址</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0,maximum-scale=1.0, user-scalable=no" />
    <link href="~/Content/css/base.css" rel="stylesheet" />
    <link rel="stylesheet" href="~/Content/css/tuoken.css" />
    <link href="~/Content/css/LArea.css" rel="stylesheet" />
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript" src="~/Content/js/base.js"></script>
    <script src="~/Content/js/layer/1.9.3/layer.js"></script>
</head>

<body>
    @*<div class="header fn-shadow">
            <a href="javascript:history.go(-1);">
                <span class="back"></span>
            </a>
            <div class="hdtitle">
                注册
            </div>
        </div>*@
    <input type="hidden" id=" id" value="@Model.id" />
    <div class="property">
        <div class="regBox">
            <div class="mod-forms">
                <div class="ui-item border-bottom clear">
                    <span class="ui-label">
                        收 货 人：
                    </span>
                    <div class="hidden">
                        <input class="ui-input" type="text" placeholder="请输入收货人姓名" value="@Model.ship_to" id="ship_to">
                    </div>
                </div>
                <div class="ui-item border-bottom clear">
                    <span class="ui-label">
                        手机号码：
                    </span>
                    <div class="hidden">
                        <input class="ui-input" type="text" placeholder="请输入收货手机号码" value="@Model.phone" id="phone">
                    </div>
                </div>
                @if (isSeas) {
                <div class="ui-item border-bottom clear">
                    <span class="ui-label">
                        真实姓名：
                    </span>
                    <div class="hidden">
                        <input class="ui-input" type="text" placeholder="请输入真实姓名" value="@Model.real_name" id="real_name">
                    </div>
                </div>
                <div class="ui-item border-bottom clear">
                    <span class="ui-label">
                        身份证号：
                    </span>
                    <div class="hidden">
                        <input class="ui-input" type="text" placeholder="请输入身份证号" value="@Model.id_card" id="id_card">
                    </div>
                </div>
                }
                <div class="ui-item border-bottom clear area">
                    <span class="ui-label">
                        所在地区：
                    </span>
                    <div class="hidden">
                        <input class="ui-input" id="demo1" type="text" readonly="" placeholder="点击选择" value="@(Model.region_name)">
                        <input id="value1" type="hidden" value="@(Model.province_id>0?Model.province_id:350200),@(Model.city_id>0?Model.city_id:350000),@(Model.area_id>0?Model.area_id:350203)">
                    </div>
                </div>
                <div class="ui-item border-bottom clear">
                    <span class="ui-label">
                        详细地址：
                    </span>
                    <div class="hidden">
                        <input class="ui-input" type="text" placeholder="请输入收货详细地址" value="@(Model.address)" id="address">
                    </div>
                </div>
                <div class="ui-item">
                    <a href="javascript:void(0)" class="settingbtn">保存地址</a>
                </div>
            </div>
        </div>
    </div>

    <script src="~/Scripts/LArea.js"></script>
    <script type="text/javascript">
        $(function () {
            $.get("/user/GetRegion", function (result) {
                if (result.result_code == 1000) {
                    var area1 = new LArea();
                    area1.init({
                        'trigger': '#demo1', //触发选择控件的文本框，同时选择完毕后name属性输出到该位置
                        'valueTo': '#value1', //选择完毕后id属性输出到该位置
                        'keys': {
                            id: 'id',
                            name: 'name'
                        }, //绑定数据源相关字段 id对应valueTo的value属性输出 name对应trigger的value属性输出
                        'type': 1, //数据源类型
                        'data': result.data //数据源
                    });
                }
            });
            $('.settingbtn').click(function () {
                if (!checkMobile()) return;
                var ship_to = $('#ship_to').val();
                var value1 = $('#value1').val();
                var adress = $('#address').val();
                var phone = $('#phone').val();
                var real_name = $("#real_name").val();
                var id_card = $("#id_card").val();
                if (ship_to.length == 0) { layer.msg = "请填写收货人姓名"; return; }
                if (adress.length == 0) { layer.msg = "请填写收货详细地址"; return; }
                if (value1.length == 0) { layer.msg = "请选择所在地区"; return; }
                var vdata = value1.split(',');
                var province_id = vdata[0];
                var city_id = vdata[1];
                var area_id;
                if (vdata.length >= 3) area_id = vdata[2];

                var data = {ship_to: ship_to, address: adress, phone: phone, province_id: province_id, city_id: city_id, area_id: area_id, id:@Model.id, real_name: real_name, id_card: id_card}
                $.ajax({
                    //几个参数需要注意一下
                    type: "POST",//方法类型
                    dataType: "json",//预期服务器返回的数据类型
                    url: "/user/SaveAdress",//url
                    data: data,
                    success: function (result) {
                        if (result.result_code == 1000) {
                            layer.msg("保存成功");
                            if (result.data.isLogin)
                                window.location.href = "/user/myadress@(!string.IsNullOrEmpty(returnUrl) ? "?returnUrl=" + returnUrl : "") ";
                            else
                                window.location.href = "/order/ConfirmOrder?addressId=" + result.data.addressId;
                        } else {
                            layer.msg(result.result_msg);
                        }
                    },
                    error: function () {
                        layer.msg("异常！");
                    }
                });

            })
        });
        function checkMobile() {
            var creg = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[89])[0-9]{8}$/;
            var treg = /^09\d{8}$/;
            var mobile = $("#phone").val();
            if (mobile.length == 0) {
                layer.msg("手机号码不能为空");
                return false;
            }
            if (!(creg.test(mobile) || treg.test(mobile))) {
                layer.msg("请输入正确的手机号");
                return false;
            }
            else return true;

        }
       


    </script>
</body>

</html>